package com.example.test1.controller;

import com.example.test1.Dto.*;
import com.example.test1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping(value = "/user", produces = "application/json")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 微信登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseDTO<Object> logIn(@RequestBody LoginDTO logInDto) throws Exception {
        Map<String, String> data = userService.logIn(logInDto.getCode());
        return new ResponseDTO<>("200", "success", data);
    }

    /**
     * 获得心得
     */

    @RequestMapping(value = "/secret", method = RequestMethod.POST)
    public ResponseDTO<Object> getSecret (@RequestBody OpenIdDTO openIdDto) {
        Set<String> res = userService.getSecret(openIdDto.getOpenId());
        return new ResponseDTO<>("200", "success", res);
    }

    /**
     * 发布心得
     */

    @RequestMapping(value = "/pubSecret", method = RequestMethod.POST)
    public ResponseDTO<Object> userSecretPublish (@RequestBody SecretDTO secretDto) {
        String res = userService.writeSecret(secretDto.getOpenId(), secretDto.getContent());
        return new ResponseDTO<>("200", "success", res);
    }

    /**
     * 删除心得
     */

    @RequestMapping(value = "/deleteSecret", method = RequestMethod.POST)
    public ResponseDTO<Object> userSecretDelete (@RequestBody SecretDTO secretDto) {
        String res = userService.deleteSecret(secretDto.getOpenId(), secretDto.getContent());
        return new ResponseDTO<>("200", "success", res);
    }
}
